-module(test_gui).

-export([init/0, init/2]).

init() ->
    init(10, 10).

init(W, H) ->
    frame:init(),
    frame ! {set_w, W},
    frame ! {set_h, H},
    setVertical(0, 0, H, fence),
    setVertical(W-1, 0, H, fence),
    setHorizontal(0, 0, W, fence),
    setHorizontal(0, H-1, W, fence),
    startRandomLoop(1000, W, H).



setVertical(_, _, 0, _) ->
    ok;
setVertical(X, Y, ToDraw, Element) ->
    frame ! {set_cell, X,Y, Element},
    setVertical(X,Y+1, ToDraw-1, Element).

setHorizontal(_, _, 0, _) ->
    ok;
setHorizontal(X, Y, ToDraw, Element) ->
    frame ! {set_cell, X,Y, Element},
    setHorizontal(X+1, Y, ToDraw-1, Element).

startRandomLoop(0, _, _) ->
    ok;
startRandomLoop(N, W, H) ->
    frame ! {set_cell, random:uniform(W-2), random:uniform(H-2), getElement(random:uniform(4))},
    frame ! {set_time,string:concat("Time: ", integer_to_list(N))},
    timer:sleep(200),
    startRandomLoop(N-1, W, H).

getElement(1) ->
    dirt;
getElement(2) ->
    grass;
getElement(3) ->
    rabbit;
getElement(4) ->
    fox;
getElement(_) ->
    dirt.




